package com.example.feifei.fragment;


import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.feifei.newproject.R;
import com.example.feifei.view.GraffitiView;

/**
 * 自定义涂鸦
 * Created by Administrator on 2018\8\12 0012.
 */

public class GraffitiFragment extends Fragment implements View.OnClickListener{
    private View root_view;

    private Button btn_undo;
    private Button btn_do;
    private Button btn_red;
    private Button btn_blue;
    private Button btn_green;
    private Button btn_scral;
    private Button btn_eraser;
    private GraffitiView graffiti_view;
    private Button btn_complete;
    private ImageView img_complete;
    private LinearLayout layout_tuya;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root_view = inflater.inflate(R.layout.fragment_graffiti,null);
        initView();
        initListener();
        return root_view;
    }

    private void initListener() {
        btn_undo.setOnClickListener(this);
        btn_do.setOnClickListener(this);
        btn_red.setOnClickListener(this);
        btn_blue.setOnClickListener(this);
        btn_green.setOnClickListener(this);
        btn_scral.setOnClickListener(this);
        btn_eraser.setOnClickListener(this);
        btn_complete.setOnClickListener(this);
    }

    /**
     * 初始化页面
     */
    private void initView(){
        graffiti_view = (GraffitiView) root_view.findViewById(R.id.graffiti_view);
        btn_undo = (Button) root_view.findViewById(R.id.btn_undo);
        btn_do = (Button) root_view.findViewById(R.id.btn_do);
        btn_red = (Button) root_view.findViewById(R.id.btn_red);
        btn_blue = (Button) root_view.findViewById(R.id.btn_blue);
        btn_green = (Button) root_view.findViewById(R.id.btn_green);
        btn_scral = (Button) root_view.findViewById(R.id.btn_scral);
        btn_eraser = (Button) root_view.findViewById(R.id.btn_eraser);
        btn_complete = (Button) root_view.findViewById(R.id.btn_complete);
        img_complete = (ImageView) root_view.findViewById(R.id.img_complete);
        layout_tuya = (LinearLayout) root_view.findViewById(R.id.layout_tuya);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_undo:
                graffiti_view.undo();
                return;
            case R.id.btn_do:
                graffiti_view.reundo();
                return;
            case R.id.btn_red:
                graffiti_view.resetPaintColor(Color.RED);
                return;
            case R.id.btn_green:
                graffiti_view.resetPaintColor(Color.GREEN);
                return;
            case R.id.btn_blue:
                graffiti_view.resetPaintColor(Color.BLUE);
                return;
            case R.id.btn_scral:
                graffiti_view.resetPaintWidth();
                break;
            case R.id.btn_eraser:
                graffiti_view.eraser();
                break;
                //完成按钮
            case R.id.btn_complete:
                Bitmap bitmap = getBitmap(layout_tuya);
                img_complete.setImageBitmap(bitmap);
                break;
        }
    }

    /**
     * 根据View获取bitmap
     * @param v
     * @return
     */
    private Bitmap getBitmap(View v) {
        if (v == null) {
            return null;
        }
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();
        Bitmap bitmap = v.getDrawingCache();
        return bitmap;
    }
}
